package com.learn.compare;


import java.util.Arrays;

public class Goods implements Comparable{

    private String name;
    private double price;

    public Goods(String name, double price) {
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Goods{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }

    @Override
    public int compareTo(Object o) {
        if(o instanceof Goods){
            Goods goods = (Goods) o;
            return Double.compare(this.price, goods.price);
        }
        throw new RuntimeException("传入数据类型不一致");
    }

    public static void main(String[] args) {
        Goods[] goods = new Goods[4];
        goods[0] = new Goods("aaa", 33);
        goods[1] = new Goods("bbb", 21);
        goods[2] = new Goods("ccc", 84);
        goods[3] = new Goods("ddd", 35);

        Arrays.sort(goods);

        System.out.println(Arrays.toString(goods));
    }
}
